package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;


    /**
     * 分页查询spu
     * @param curPage 当前页码
     * @param rows 每页大小
     * @param bid 品牌id
     * @param cid 商品分类id
     * @param sid 商品spu的ID
     * @param saleable 是否上架
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageSpu(
            @RequestParam(value = "page",defaultValue = "1")Long curPage,
            @RequestParam(value = "rows",defaultValue = "5")Long rows,
            @RequestParam(value = "brandId",required = false)Long bid,
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "id",required = false)Long sid,
            @RequestParam(value = "saleable",required = false)Boolean saleable
    ){
        return ResponseEntity.ok(goodsService.pageSpu(curPage,rows,bid,cid,sid,saleable));
    }

    /**
     * 新增商品
     * @param spuDTO 商品信息，包含sku和spuDetail的信息
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> saveSpu(@RequestBody SpuDTO spuDTO){
        goodsService.saveSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品上下架
     * @param sid 要修改商品的id
     * @param saleable 是否上架
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam(value = "id",required = false) Long sid,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        goodsService.updateSpuSaleable(sid,saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id sid
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.queryGoodsById(id));
    }


    /**
     * 更新商品
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO){
        goodsService.updateSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据商品sid查询商品细节
     * @param sid
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailBySid(@RequestParam("id") Long sid){
        return ResponseEntity.ok(goodsService.queryDetailBySid(sid));
    }

    /**
     * 根据spuID查询sku的集合
     * @param sid 商品id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySid(@RequestParam("id") Long sid){
        return ResponseEntity.ok(goodsService.listSkuBySid(sid));
    }

    /**
     * 根据id批量查询sku
     * @param skuIds  sku的id集合
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuBySkuIds(@RequestParam("ids") List<Long> skuIds){

        return ResponseEntity.ok(goodsService.listSkuBySkuIds(skuIds));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param sid 商品id
     * @param isSearching 是否参与搜索
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long sid,
            @RequestParam(value = "searching",required = false)Boolean isSearching
            ){
        return ResponseEntity.ok(goodsService.querySpecsValues(sid,isSearching));
    }


    /**
     * 根据id查询商品
     * @param id 商品id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long sid){
        return ResponseEntity.ok(goodsService.querySpuById(sid));
    }


}